package renguanyu.app.tools.controller;

import java.util.ArrayList;
import java.util.List;

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;

public class HeaderToMapController {

	@FXML
	TextArea headerSource;
	@FXML
	TextArea stringbuilderTarget;

	@FXML
	public void headerToMap() {
		List<String> result = new ArrayList<>();
		String sqlStr = headerSource.getText();
		String[] sqlArray = sqlStr.split("\n");
		String tempVar = "headers";
		result.add("Map<String, String> " + tempVar + " = new HashMap<>();");
		for (int i = 0; i < sqlArray.length; i++) {
			String s = sqlArray[i];
			if (!s.contains(": ")) {
				continue;
			}
			int idx1 = s.indexOf(": ");
			String k = s.substring(0, idx1);
			String v = s.substring(idx1 + 1).trim();
			result.add("" + tempVar + ".put(\"{k}\",\"{v}\");".replace("{k}", k).replace("{v}", v));
		}
		stringbuilderTarget.clear();
		result.forEach(data -> {
			stringbuilderTarget.appendText(data);
			stringbuilderTarget.appendText("\n");
		});
	}

}
